package com.tensquare.friend.controller;

import com.tensquare.friend.service.FriendService;
import entity.Result;
import entity.StatusCode;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/friend")
public class FriendController {
    @Autowired
    private FriendService friendService;
    @Autowired
    private HttpServletRequest request;

    @RequestMapping(value = "/like/{friendid}/{type}", method = RequestMethod.PUT)
    public Result like(@PathVariable String friendid, @PathVariable Integer type) {

        //获得JwtToken的载荷部分
        Claims claims = (Claims) request.getAttribute("claims_user");
        //判断载荷是否为空
        if (claims == null) { //未登录
            return new Result(false, StatusCode.ERROR, "请先登录");
        }
        //获得当前登录用户id
        String userid = claims.getId();

        if (type == 1) { //喜欢

            friendService.addFriend(userid, friendid);

        } else {//不喜欢

            friendService.addNoFriend(userid, friendid);
        }

        return new Result(true, StatusCode.OK, "操作成功");
    }

    @RequestMapping(value = "/{friendid}", method = RequestMethod.DELETE)
    public Result delete(@PathVariable String friendid) {
        //获得JwtToken的载荷部分
        Claims claims = (Claims) request.getAttribute("claims_user");
        //判断载荷是否为空
        if (claims == null) { //未登录
            return new Result(false, StatusCode.ERROR, "请先登录");
        }
        //获得当前登录用户id
        String userid = claims.getId();
        //删除好友
        friendService.delFriend(userid, friendid);

        return new Result(true, StatusCode.OK, "操作成功");
    }

}
